using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MSIACCSL2.ViewModels;
using WcfService1.DTO;

namespace MSIACCSL2.Views.SalesOrder
{
    public partial class SearchItem : ChildWindow
    {
        public dynamic ViewModel
        {
            get
            {
                return this.DataContext as dynamic;
            }
        }

        private bool _isInitialLoad = true;

        
        public SearchItem()
        {
            InitializeComponent();
            this.txtSearch.KeyDown += new KeyEventHandler(txtSearch_KeyDown);
            this.dg1.KeyUp += new KeyEventHandler(dg1_KeyUp);
            this.Loaded +=new RoutedEventHandler(SearchItem_Loaded);
            this.GotFocus += new RoutedEventHandler(SearchItem_GotFocus);
        }

      

        void SearchItem_GotFocus(object sender, RoutedEventArgs e)
        {
            if (_isInitialLoad)
            {
                txtSearch.Focus();
                _isInitialLoad = false;
            }
        }

        void SearchItem_Loaded(object sender, RoutedEventArgs e)
        {
            _isInitialLoad = true;
        }

   
        void dg1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dg1.SelectedItem != null)
            {
                txtSearch.Text = ((ItemDTO)dg1.SelectedItem).ItemName;
                if (ViewModel.GetType() == typeof(SalesOrderViewModel))
                    ViewModel.SalesOrderDetailCurrent.Item = ((ItemDTO)dg1.SelectedItem);
                //else if (ViewModel.GetType()==typeof(BTBViewModel))
                //{
                //    ViewModel.BtbDetailCurrent.Item = ((ItemDTO)dg1.SelectedItem);
                //    ((BTBDetailDTO) ViewModel.BtbDetailCurrent).SatuanUnit = ((ItemDTO) dg1.SelectedItem).Satuan;
                //}
                else if (ViewModel.GetType() == typeof(BtbElseViewModel))
                {
                    ViewModel.BtbElseDetailCurrent.Item = ((ItemDTO)dg1.SelectedItem);
                    ((BtbElseDetailDTO)ViewModel.BtbElseDetailCurrent).SatuanUnit = ((ItemDTO)dg1.SelectedItem).Satuan;
                }
                else if (ViewModel.GetType() == typeof(SpkViewModel))
                {
                    ViewModel.SpkDetailCurrent.Item = ((ItemDTO)dg1.SelectedItem);
                }
                else if (ViewModel.GetType() == typeof(InventoryAdjustmentViewModel))
                {
                    ViewModel.InventoryAdjustmentDetailCurrent.Inventory.Item = ((ItemDTO)dg1.SelectedItem);
                    ((InventoryAdjustmentDetailDTO)ViewModel.InventoryAdjustmentDetailCurrent).Inventory.SatuanUnit = ((ItemDTO)dg1.SelectedItem).Satuan;
                }
                
            }
        }

        void dg1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (dg1.SelectedIndex != 0) dg1.SelectedIndex -= 1;
                this.Close();
            }
            else if (e.Key == Key.Escape)
            {
                txtSearch.Focus();
            }
        }

        private void txtSearch_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ViewModel.GetItemByName((sender as TextBox).Text);
            }
            else if (e.Key == Key.Down)
            {
                dg1.Focus();
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

